import { useUserStore } from '@/store/modules/user.ts'

// 验证用户是否含有指定权限，只需包含其中一个
export const hasPermiOr = (permissions) => {
  console.log('-----------------------------1')
  return permissions.some((item) => {
    return authPermission(item)
  })
}

export const hasRole = (role) => {
  console.log('-----------------------------1')
  console.log(role)
  return authRole(role)
}

function authPermission(permission) {
  let userStore = useUserStore()
  const all_permission = '*:*:*'
  const permissions = userStore.permissions
  console.log('-----------------------------')
  console.log(permission)
  console.log(permissions)
  if (permission && permission.length > 0) {
    return permissions.some((v) => {
      return all_permission === v || v === permission
    })
  } else {
    return false
  }
}

function authRole(role) {
  const super_admin = 'admin'
  let userStore = useUserStore()
  const roles = userStore.roles
  if (role && role.length > 0) {
    return roles.some((v) => {
      return super_admin === v || v === role
    })
  } else {
    return false
  }
}
